#!/bin/bash

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

read -p "Enter mysql version (default 8.0 e.g. 5.7 | 8.0): " version </dev/tty
version=$(echo "$version" | tr -s '[:space:]')
if [[ "$version" == "" ]]; then version="8.0"; fi

$SUDO mkdir -p /opt/mysql/${version}

if [[ -x /opt/mysql/$version/bin/mysqld ]]; then
    read -p "mysql[${version}] is installed, reinstall it? (y/N) " reinstall </dev/tty
    reinstall=$(echo "$reinstall" | tr -s '[:space:]')
    if [[ "$reinstall" != [Yy] ]]; then exit 0; fi
fi
$SUDO rm -rf /opt/mysql/${version}/*
$SUDO rm -rf /etc/systemd/system/mysql-${version}.service
$SUDO rm -rf mysql_${version}.tar.*

ps -ef | grep mysqld | grep -v grep
if [[ $? == 0 ]]; then echo -e "\n[ERROR] please stop mysql service at first."; exit -1; fi

$SUDO apt install libncurses5 libaio1 libnuma1 -y

read -p "Enter mysql[${version}] listen port (default 3306): " mysql_port </dev/tty
mysql_port=$(echo "$mysql_port" | tr -s '[:space:]')
if [[ "$mysql_port" == "" ]]; then mysql_port="3306"; fi

case $version in
    5.7)
        wget https://mirrors.huaweicloud.com/artifactory/mysql-local/Downloads/MySQL-5.7/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -O mysql_${version}.tar.gz
        $SUDO tar zxvf mysql_${version}.tar.gz -C /opt/mysql/${version} --strip-components=1
        rm -f mysql_${version}.tar.gz
        ;;
    8.0)
        wget https://mirrors.huaweicloud.com/artifactory/mysql-local/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.17-x86_64-minimal.tar.xz -O mysql_${version}.tar.xz
        $SUDO tar xvf mysql_${version}.tar.xz -C /opt/mysql/${version} --strip-components=1
        rm -f mysql_${version}.tar.xz
        ;;
    *)
        echo "error version"
        exit -1
esac

$SUDO mkdir -p /opt/mysql/${version}/{data,logs}

/opt/mysql/${version}/bin/mysql --version
if [[ $? != 0 ]]; then echo -e "\n[ERROR] please resolve mysql deps with '/opt/mysql/${version}/bin/mysql --version'."; exit -1; fi

cat << EOF | $SUDO tee /opt/mysql/${version}/my.ini
[mysqld]
basedir=/opt/mysql/${version}
datadir=/opt/mysql/${version}/data
log-error=/opt/mysql/${version}/logs/error.log
pid-file=/opt/mysql/${version}/mysql.pid

port=${mysql_port}

# skip-grant-tables
max_allowed_packet=1024M
default-time-zone='+08:00'
innodb_file_per_table=1
default-storage-engine=innodb
character_set_server=utf8mb4
collation-server=utf8mb4_unicode_ci

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4
EOF

$SUDO groupadd mysql
$SUDO useradd mysql -g mysql
$SUDO chown -R mysql:mysql /opt/mysql/${version}
$SUDO /opt/mysql/${version}/bin/mysqld --defaults-file=/opt/mysql/${version}/my.ini --user=mysql --initialize
$SUDO chmod 777 /opt/mysql/${version}/logs/error.log
$SUDO /opt/mysql/${version}/bin/mysqld --defaults-file=/opt/mysql/${version}/my.ini --user=mysql --daemonize
temppass=$(awk '/temporary password/ {print $NF}' /opt/mysql/${version}/logs/error.log)
if [[ -z $temppass ]]; then echo "mysql is failured to install"; exit 1; fi

$SUDO /opt/mysql/${version}/bin/mysql -hlocalhost -P${mysql_port} -uroot -p${temppass} --connect-expired-password -e "alter user 'root'@'localhost' identified with mysql_native_password by '654321'; grant all privileges on *.* to 'root'@'localhost' with grant option; flush privileges;"

$SUDO cat /opt/mysql/${version}/mysql.pid | $SUDO xargs kill

cat << EOF | $SUDO tee /etc/systemd/system/mysql-${version}.service
[Unit]
Description=MySQL Service - ${version}

[Service]
Restart=on-failure
RestartSec=30s
User=mysql
ExecStart=/opt/mysql/${version}/bin/mysqld --defaults-file=/opt/mysql/${version}/my.ini --user=mysql

[Install]
WantedBy=multi-user.target
Alias=mysql-${version}.service
EOF

echo -e "\nMake sure that the following content exists in the \$HOME/.bashrc"
echo -e "\texport MYSQL_HOME=/opt/mysql/${version}"
echo -e "\texport PATH=\$MYSQL_HOME/bin:\$PATH"
echo -e "\n\tsudo systemctl start mysql-${version}"
